package com.nononsenseapps.feeder.pad.ui.reader.sample

import android.os.Bundle
import android.view.View
import androidx.navigation.fragment.findNavController
import com.billy.android.swipe.SmartSwipe
import com.billy.android.swipe.SmartSwipeWrapper
import com.billy.android.swipe.SwipeConsumer
import com.billy.android.swipe.consumer.BezierBackConsumer
import com.billy.android.swipe.listener.SimpleSwipeListener
import com.nononsenseapps.feeder.pad.R
import com.nononsenseapps.feeder.ui.AbstractFeedActivity
import com.nononsenseapps.feeder.ui.FabClickListener
import com.nononsenseapps.feeder.ui.reader.sample.AbstractReaderFragment
import com.nononsenseapps.feeder.ui.reader.webview.ARG_URL
import com.nononsenseapps.feeder.util.*
import timber.log.Timber

const val DIALOG_MODEL_FLAG="dialog_model"

class ReaderFragment :  AbstractReaderFragment() {

    var dialogModel:Boolean=false

    override fun onActivityCreated(bundle: Bundle?) {
        super.onActivityCreated(bundle)
        Timber.d("call on activityCreated ..")
        arguments?.let {
            dialogModel=it.getBoolean(DIALOG_MODEL_FLAG,false)
        }
        if(!dialogModel) {
            (activity as AbstractFeedActivity).initFab(com.nononsenseapps.feeder.ui.R.drawable.ic_forward_next,object: FabClickListener {
                override fun fabOnClickListener() {
                    moveNext()
                }

                override fun fabOnDoubleClickListener() {
                    movePref()
                }
            })
        }
    }

    override fun getLayoutId(): Int {
        return when  {
            dialogModel-> R.layout.fragment_reader_pop
            !ScreenUtils.isPortrait(context)->R.layout.fragment_reader_tablet
            else ->R.layout.fragment_reader
        }
       /* return if (dialogModel||!ScreenUtils.isPortrait(context)) {
            R.layout.fragment_reader_tablet
        } else {
            R.layout.fragment_reader
        }*/
    }

    override fun onResume() {
        super.onResume()
    }

    override fun buildSwipeView(inView: View): View {
        val swipeRootView = SmartSwipe.wrap(inView)
        if(dialogModel) {
            swipeRootView.addConsumer(BezierBackConsumer())
                    .enableVertical()
                    .enableNestedFlyAllDirections(false)
                    .setOpenDistance(230)
                    .addListener(object : SimpleSwipeListener() {
                        override fun onSwipeOpened(wrapper: SmartSwipeWrapper?, consumer: SwipeConsumer?, direction: Int) {
                            Timber.d("swipe direction:%d", direction)
                            when (direction) {
                                SwipeConsumer.DIRECTION_TOP -> markAsFavorite()
                                SwipeConsumer.DIRECTION_BOTTOM -> activity?.onBackPressed()
                            }
                        }
                    })
        }else{
            swipeRootView.addConsumer(BezierBackConsumer())
                    .enableTop()
                    .enableNestedFlyAllDirections(false)
                    .setOpenDistance(200)
                    .addListener(object : SimpleSwipeListener() {
                        override fun onSwipeOpened(wrapper: SmartSwipeWrapper?, consumer: SwipeConsumer?, direction: Int) {
                            Timber.d("swipe direction:%d", direction)
                            when (direction) {
                                SwipeConsumer.DIRECTION_TOP ->openInWebView()
                            }
                        }
                    })
        }
        return swipeRootView
    }

    override fun openInWebView(){
       getCurrentFeedItem()?.let { rssItem ->
           rssItem.link?.let { link ->
               findNavController().navigate(
                          if(!dialogModel)  R.id.action_readerFragment_to_readerWebViewFragment else  R.id.action_feedFragment_to_readerWebViewFragmentDialog,
                           bundle {
                               putString(ARG_URL, link)
                               putString(ARG_ENCLOSURE, rssItem.enclosureLink)
                           })
           }
       }
   }

    override fun debug(): Boolean =false

    override fun navigateToCodeViewFragment() {
        if(!dialogModel)
           findNavController().navigate(R.id.action_readerFragment_to_codeViewFragment)
    }

    override fun navigateToWebViewFragment(link: String) {
        if(!dialogModel)
           findNavController().navigate(R.id.action_readerFragment_to_readerWebViewFragment, bundle {
            putString(ARG_URL, link)
          })
    }
}

